package cn.com.ylpw.web.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @description 
 * 功能描述: 页面相关工具类
 * @author 		  作         者: 张嘉杰
 * @createdate   建立日期：Aug 21, 201310:59:05 AM
 * @projectname  项目名称: ylpwweb
 * @packageclass 包及类名: cn.com.ylpw.common.HtmlUtils.java
 */
public class HtmlUtils {
	
	 public static final Logger log = LoggerFactory.getLogger(HtmlUtils.class);
	
	/**
	 * @description 
	 * 功能描述: 设置使用代理服务器
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: ipss [192.168.1.1:8888]
	 * @return       返回类型: 无
	 * @createdate   建立日期：Aug 21, 201311:05:04 AM
	 */
	public static void initProxy(String ipss) {  
		Properties prop = System.getProperties();   
		prop.put("proxySet", "true");   //设置http访问要使用的代理服务器的地址 
		String[] ip = ipss.split(":");
		// 设置http访问要使用的代理服务器的地址   
	    prop.setProperty("http.proxyHost", ip[0]);   
	    // 设置http访问要使用的代理服务器的端口   
	    prop.setProperty("http.proxyPort", ip[1]);
	}
	
	/**
	 * @description 
	 * 功能描述: 获取HTML响应文本
	 * @author 		  作         者: 张嘉杰
	 * @param	             参         数: 
	 * @return       返回类型: 
	 * @createdate   建立日期：Aug 21, 201311:08:17 AM
	 */
	public static String getHtmlContent(String htmlUrl, boolean bool, String... charset){
		HttpURLConnection con = null;
		InputStream is = null;
		InputStreamReader ireader = null;
		BufferedReader breader = null;
		try {
			URL url = new URL(htmlUrl);
			con = (HttpURLConnection) url.openConnection();
			// Firefox浏览
			con.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
			int index = htmlUrl.indexOf("/", 10);
			con.setRequestProperty("Host", index == -1 ? htmlUrl.substring(7) : htmlUrl.substring(7, index));
			is = con.getInputStream();
			String encoding = "utf-8";
			if (bool && charset.length==0) {
				// 尝试从http头中获取字符集
				String contentType = con.getHeaderField("Content-Type").toLowerCase();
				boolean charsetFound = false;
				if (contentType.contains("charset")) {
					encoding = contentType.split("charset=")[1];
					charsetFound = true;
				}
				// 如果没有,读取头1024个字符，检查html的header
				byte[] buf = new byte[1024];
				if (!charsetFound) {
					int len = is.read(buf);
					while (len <= 32) {
						len = is.read(buf);
					}
					String header = new String(buf, 0, len);
					Pattern p = Pattern.compile(".*<meta.*content=.*charset=.*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
					Matcher m = p.matcher(header);
					if (m.matches()) {
						encoding = header.toLowerCase().split("charset=")[1].replaceAll("[^a-z|1-9|\\-]", " ").split("\\s+")[0];
					} else {
						encoding = "gbk";
					}
				}
			} else {
				encoding = charset[0];
			}
			ireader = new InputStreamReader(is, encoding);
			breader = new BufferedReader(ireader);
			StringBuilder b = new StringBuilder();
			String line;
			while ((line = breader.readLine()) != null) {
				b.append(line);
				b.append("\r\n");
			}
			return b.toString();
		} catch (FileNotFoundException e) {
			log.error("找不到页面:{}",htmlUrl,e);
			return null;
		} catch (ConnectException e) {
			log.error("访问超时:{}", htmlUrl,e);
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			try {
				if(is!=null){
					is.reset();
					is.close();
				}
				if(ireader!=null){
					ireader.reset();
					ireader.close();
				}
				if(breader!=null){
					breader.reset();
					breader.close();
				}
				if(con!=null){
					con.disconnect();
				}
			} catch (Exception e2) {
			}
		}
	}
	
}
